package com.leostadyn.stackfarm.timestatistics.view.activity.admin.project;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.adapter.ProjectListAdapter;
import com.leostadyn.stackfarm.timestatistics.base.BaseActivity;
import com.leostadyn.stackfarm.timestatistics.common.Constants;
import com.leostadyn.stackfarm.timestatistics.model.bean.DeleteBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.ProjectBean;
import com.leostadyn.stackfarm.timestatistics.net.api.AdminApi;
import com.leostadyn.stackfarm.timestatistics.net.base.OriginalSubscriber;
import com.leostadyn.stackfarm.timestatistics.net.base.ResponseThrowable;
import com.leostadyn.stackfarm.timestatistics.utils.MyDialog;
import com.leostadyn.stackfarm.timestatistics.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class ProjectListActivity extends BaseActivity implements View.OnClickListener, OnRefreshListener {
    ImageView nav_back;
    TextView add;
    TextView nav_title;
    RecyclerView rv_project;
    ProjectListAdapter adapter;
    SmartRefreshLayout smartRefreshLayout;
    LinearLayout nodata_layout;
    ArrayList<ProjectBean> projectBeans = new ArrayList<>();
    static HashMap<String, String> map = new HashMap<>();
    private int mPage = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project_list);

        initView();


    }

    public static final int REQUESTCODE = 100;


    public void onProjectList() {

        map.put("page", String.valueOf(mPage));
        map.put("pageSize", String.valueOf(Constants.PAGESIZE));

        final KProgressHUD dialog = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setCancellable(true).setAnimationSpeed(2).setDimAmount(0.5f);
        dialog.show();

        AdminApi.getInstance().getProjectList(map).subscribe(new OriginalSubscriber<List<ProjectBean>>(compositeDisposable) {
            @Override
            public void onError(ResponseThrowable e) {
                dialog.dismiss();
                ToastUtil.show(ProjectListActivity.this, e.getMsg());
            }

            @Override
            public void onNext(List<ProjectBean> projectBeans, String msg) {
                if (smartRefreshLayout != null) {
                    smartRefreshLayout.finishLoadMore();
                    smartRefreshLayout.finishRefresh();
                }
                dialog.dismiss();

                if (mPage == 1) {
                    smartRefreshLayout.setEnableLoadMore(true);
                    adapter.setProjectListBeans(projectBeans);
                    if (projectBeans == null || projectBeans.size() == 0) {
                        nodata_layout.setVisibility(View.VISIBLE);
                        rv_project.setVisibility(View.GONE);
                    } else {
                        nodata_layout.setVisibility(View.GONE);
                        rv_project.setVisibility(View.VISIBLE);
                    }
                } else {
                    adapter.setProjectListBeans(projectBeans);
                }
                if (projectBeans != null) {
                    if (projectBeans.size() < Constants.PAGESIZE) {
                        smartRefreshLayout.setEnableLoadMore(false);
                    } else {
                        mPage++;
                    }
                }
            }
        });
    }

    public void initView() {

        nav_back = findViewById(R.id.nav_img_back);
        nav_back.setOnClickListener(v -> {
            ProjectListActivity.this.finish();
        });

        nav_title = findViewById(R.id.nav_txt_title);
        nav_title.setText("项目列表");

        add = findViewById(R.id.nav_btn_add);
        add.setVisibility(View.VISIBLE);
        add.setOnClickListener(this);

        nodata_layout = findViewById(R.id.layout_no_data);


        rv_project = findViewById(R.id.rv_project);
        if (projectBeans == null || projectBeans.size() == 0) {
            nodata_layout.setVisibility(View.VISIBLE);
            rv_project.setVisibility(View.GONE);
        } else {
            nodata_layout.setVisibility(View.GONE);
            rv_project.setVisibility(View.VISIBLE);
        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_project.setLayoutManager(linearLayoutManager);
        rv_project.setAdapter(adapter = new ProjectListAdapter(ProjectListActivity.this, projectBeans, new ProjectListAdapter.OnDeleteButtonClick() {
            @Override
            public void onBtnClicked(int position, ProjectBean data, TextView name, TextView state, TextView id, TextView time) {

                dialogShow(data);
            }
        }));

        smartRefreshLayout = findViewById(R.id.project_srl_area);
        smartRefreshLayout.setOnRefreshListener(this);
        onProjectList();

    }

    public void dialogShow(ProjectBean data) {

        View view = getLayoutInflater().inflate(R.layout.layout_dialog, null);
        TextView dialog_theme = view.findViewById(R.id.dialog_theme);
        TextView dialog_delete = view.findViewById(R.id.dialog_delete);
        TextView dialog_cancel = view.findViewById(R.id.dialog_cancel);
        dialog_theme.setText("是否删除该项目？");
        final Dialog dialog = new MyDialog(ProjectListActivity.this, 0, 0, view, R.style.DialogTheme);
        dialog.setCancelable(true);
        dialog.show();
        dialog_delete.setOnClickListener(v -> {

            deleteData(data);
            dialog.dismiss();

            onProjectList();
        });
        dialog_cancel.setOnClickListener(v -> {
            dialog.dismiss();
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nav_btn_add:

                startActivity(new Intent(this, ProjectAddActivity.class));
                break;

        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        onProjectList();
    }

    private void deleteData(ProjectBean data) {
        HashMap<String, String> deletemap = new HashMap<>();
        deletemap.put("id", data.getId());
        AdminApi.getInstance().deleteProjectData(deletemap).subscribe(new OriginalSubscriber<DeleteBean>(compositeDisposable) {
            @Override
            public void onError(ResponseThrowable e) {
                //ToastUtil.show(StaffListActivity.this, e.getMsg());
                ToastUtil.show(ProjectListActivity.this, "该项目有工时，无法删除！");
            }

            @Override
            public void onNext(DeleteBean deleteBean, String msg) {
                ToastUtil.show(ProjectListActivity.this, "删除成功");
                onProjectList();
            }
        });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        onProjectList();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUESTCODE && resultCode == Activity.RESULT_OK) {
            mPage = 1;
            onProjectList();
        }
    }
}
